package com.yanqu.road.dao.impl.activity.busyfarming;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.busyfarming.BusyFarmingLand;
import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class BusyFarmingUserDataDaoImpl extends TempDataObjectDao<BusyFarmingUserData> {


    public static String getBusyFarmingUserDataSql = "select * from t_u_activity_busy_farming_user_data where activityId=?";
    public Map<Long, BusyFarmingUserData> getBusyFarmingUserData(int activityId) {
        Map<Long, BusyFarmingUserData> result = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBusyFarmingUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusyFarmingUserData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertBusyFarmingUserDataSql = "insert into t_u_activity_busy_farming_user_data(userId,activityId,homeGrade,fruitScore,wateringTimes,beWateringTimes,beStealTimes,stealTimes,landMap,resetTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(BusyFarmingUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHomeGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFruitScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWateringTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeWateringTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeStealTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStealTimes()));
        if(data.getLandMap() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getLandMap())));
        }
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        if (getDb().executeNoneQuery(insertBusyFarmingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateBusyFarmingUserDataSql = "update t_u_activity_busy_farming_user_data set homeGrade=?,fruitScore=?,wateringTimes=?,beWateringTimes=?,beStealTimes=?,stealTimes=?,landMap=?,resetTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(BusyFarmingUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getHomeGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFruitScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWateringTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeWateringTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeStealTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStealTimes()));
        if(data.getLandMap() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getLandMap())));
        }
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateBusyFarmingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusyFarmingUserData parseData(ResultSet rs) throws SQLException {
        BusyFarmingUserData data = new BusyFarmingUserData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setHomeGrade(rs.getInt("homeGrade"));
        data.setFruitScore(rs.getLong("fruitScore"));
        data.setWateringTimes(rs.getInt("wateringTimes"));
        data.setBeWateringTimes(rs.getInt("beWateringTimes"));
        data.setBeStealTimes(rs.getInt("beStealTimes"));
        data.setStealTimes(rs.getInt("stealTimes"));
        data.setResetTime(rs.getLong("resetTime"));
        String landMap = rs.getString("landMap");
        if(!StringUtils.isNullOrEmpty(landMap)){
            Map<Integer, BusyFarmingLand> landMapData = JSON.parseObject(landMap, new TypeReference<Map<Integer, BusyFarmingLand>>() {});
            if(landMapData != null){
                data.setLandMap(landMapData);
            }
        }
        return data;
    }
}
